package com.atguigu.serviceedu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.atguigu.serviceedu.entity.EduSubject;
import com.atguigu.serviceedu.entity.ExcelSubject.ExcelSubjectData;
import com.atguigu.serviceedu.entity.vo.EduSubjectVO;
import com.atguigu.serviceedu.listener.ExcelSubjectDataListener;
import com.atguigu.serviceedu.mapper.EduSubjectMapper;
import com.atguigu.serviceedu.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2023-07-21
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    @Autowired
    private EduSubjectMapper eduSubjectMapper;
    @Override
    public void batchimportsubjects(InputStream inputStream) {
        EasyExcel.read(inputStream, ExcelSubjectData.class,new ExcelSubjectDataListener(eduSubjectMapper)).excelType(ExcelTypeEnum.XLS).sheet().doRead();
    }

    @Override
    public List<EduSubjectVO> queryNestedListByParentId(String parentId) {
        List<EduSubjectVO> eduSubjectVOS = eduSubjectMapper.selectNestedListByParentId(parentId);
        return  eduSubjectVOS;
    }

    @Override
    public List<EduSubjectVO> queryNestedListByParentIdForSite(String parentId) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId);
        List<EduSubject> subjectList = eduSubjectMapper.selectList(wrapper);
        List<EduSubjectVO>subjectVOList=new ArrayList<>();
        for (int i=0;i<subjectList.size();i++){
            EduSubject eduSubject = subjectList.get(i);
            EduSubjectVO eduSubjectVO = new EduSubjectVO();
            BeanUtils.copyProperties(eduSubject,eduSubjectVO);

            QueryWrapper<EduSubject> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("parent_id",eduSubject.getId());
            List<EduSubject> subjectList2= eduSubjectMapper.selectList(wrapper2);
            eduSubjectVO.setChildren(subjectList2);
            subjectVOList.add(eduSubjectVO);
        }
        return subjectVOList;
    }
}
